const componentString = "TableFormModal , Assign,AssignSearch,CheckboxTitle, ys_Handsontable, RuleDom";


const rootPath = 'D:\\WorkStation\\projects\\develop-ncc1.0\\src';
const path = require("node:path");
const fs = require("node:fs");
/**
 * 
 * @param compNameStr {string}
 */
function generateImortInfo(compNameStr) {
	const compNames = compNameStr.split(",").map(n => n.trim());
	const imports = compNames.map(n => `import ${n} from "epmp/common/components/${n}"`);
	const moduleAndDependJs = findDependJssAndModuleName(compNames);
	return {
		imports: imports.join(';\n')+';',
		configJSON: `{
  "dependModuleName": [\n    ${moduleAndDependJs.dependModuleName.map(n => `"${n}"`).join(',\n    ')}\n  ],
  "dependjs": [\n    ${moduleAndDependJs.dependjs.map(n => `"${n}"`).join(',\n    ')}\n  ]	
}`
	}
}

/**
 * 
 * @param compNames {Array}
 */
function findDependJssAndModuleName(compNames){
	const totalDependjs = new Set();
	const totalDependModuleName = new Set();
	for (const compName of compNames) {
		const compModuleName = `epmp/common/components/${compName}`;
		const compPath = path.resolve(rootPath, `epmp/common/components/${compName}`);
		const compConfigPath = `${compPath}/config.json`;
		const configJson = require(compConfigPath);
		const dependjs = configJson.dependjs;
		for (const djs of dependjs) {
			totalDependjs.add(djs);
		}
		totalDependjs.add(`../../../../${compModuleName}/index.js`);
		totalDependModuleName.add(compModuleName);
	}
	console.log(Array.from(totalDependjs));
	return {
		dependModuleName: Array.from(totalDependModuleName),
		dependjs: Array.from(totalDependjs)
	}
}

const res = generateImortInfo(componentString);
console.log(res);
const resPath = path.resolve(__dirname, 'res.txt');
fs.writeFile(resPath, `
imports:
${res.imports}



configJson:
${res.configJSON}
`, () => {});



